package base.controller;

import com.alibaba.fastjson.JSONObject;
import demo.utils.EhcacheUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author : GuoFei
 * @date : 2018/11/5 9:33
 */
@Controller
@RequestMapping("ehcache")
public class EhcacheController {

    @RequestMapping
    @ResponseBody
    public JSONObject contro() {
        return new JSONObject();
    }

    @RequestMapping("/showUser")
    @ResponseBody
    public String toIndex(HttpServletRequest request, Model model) {
        System.out.println("UserController showUser");
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println("showUser");
        String test = "showUser";
        //将查询到的结果放入缓存
        EhcacheUtil.getInstance().put("ehcacheGO", "userEch", test);
        model.addAttribute("user", test);
        return "showUser";
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public String toEhcahe(HttpServletRequest request, Model model) {
        System.out.println("use cacher");
        String test = (String) EhcacheUtil.getInstance().get("ehcacheGO", "userEch");
        //从缓存中取数据
        model.addAttribute("userCache", test);
        model.addAttribute("user", test);
        return "showUser";
    }

}
